local cangshi = fk.CreateSkill {
  name = "lingling__cangshi",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable {
  ["lingling__cangshi"] = "仓实",
  [":lingling__cangshi"] = "限定技，出牌阶段，或当你死亡时，你可以令一名角色摸五张牌，且其跳过下一个弃牌阶段。",

  ["#lingling__cangshi"] = "仓实：令一名角色摸五张牌并跳过下一个弃牌阶段！",

  ["$lingling__cangshi1"] = "仓廪实，礼义兴。岁丰民安，天下太平。",
  ["$lingling__cangshi2"] = "粟乃实廪，豆不掩肩。",
}

cangshi:addEffect("active", {
  anim_type = "support",
  prompt = "#lingling__cangshi",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(cangshi.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local target = effect.tos[1]
    target:drawCards(5, cangshi.name)
    if not target.dead then
      room:setPlayerMark(target, cangshi.name, 1)
    end
  end,
})

cangshi:addEffect(fk.Death, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(cangshi.name, false, true) and target == player and
        player:usedSkillTimes(cangshi.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    print("3333333333")
    local _, dat = room:askToUseActiveSkill(player, {
      skill_name = "lingling__cangshi",
      prompt = "#lingling__cangshi",
      cancelable = true,
      skip = true
    })
    if dat then
      event:setCostData(self, { tos = dat.targets, cards = dat.cards, from = player })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local skill = Fk.skills[cangshi.name]
    if skill then
      skill:onUse(player.room, event:getCostData(self))
    end
  end
})

cangshi:addEffect(fk.EventPhaseChanging, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark(cangshi.name) > 0 and data.phase == Player.Discard
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, cangshi.name, 0)
    data.skipped = true
  end,
})

return cangshi
